/*
 * QTXpost - a Qt-based blog client.
 * Copyright (C) 2010 Helen Sommer
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program (in COPYING); if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA  02111-1307  USA
 *
 * The project is hosted on http://code.google.com/p/qtxpost
 */

#include "EditUser.h"
#include "MainWindow.h"

extern MainWindow *root;

EditUser::EditUser(QWidget *parent) 
  : QDialog(parent)
{
  setupUi(this);

  connect(cancelButton, SIGNAL(clicked()), this, SLOT(close()));
  connect(okButton, SIGNAL(clicked()), this, SLOT(update()));
  connect(foregroundButton, SIGNAL(clicked()), this, SLOT(selectForeground()));
  connect(backgroundButton, SIGNAL(clicked()), this, SLOT(selectBackground()));
  
  watchedToggle->setEnabled(false);
  trustedToggle->setEnabled(false);
  usernameLine->setReadOnly(true);
  fullnameLine->setReadOnly(true);
  
}

void EditUser::initialize(QString username_)
{
  QSqlQuery query;
  QString command;

  username = username_;
  
  command = QString("select fullname, trust, trusted, watch, watched, fgcolor, bgcolor, birthday from friends where users_id=%1 and username=\"%2\"").arg(root->friendsManager->server->id).arg(username);
  query.exec(command);
  if (!query.next()) { // shouldn't happen
    // user not found - db inconsistent
    return;
  }

  QString fullname = query.value(0).toString();
  trust = query.value(1).toBool();
  bool trusted = query.value(2).toBool();
  watch = query.value(3).toBool();    
  bool watched = query.value(4).toBool();   
  foreground = query.value(5).toString();
  background = query.value(6).toString();
  QString birthday = query.value(7).toString();

  usernameLine->setText(username);
  fullnameLine->setText(fullname);
  trustToggle->setChecked(trust);
  trustedToggle->setChecked(trusted);
  watchToggle->setChecked(watch);
  watchedToggle->setChecked(watched);
  foregroundLine->setText(foreground);
  backgroundLine->setText(background);
  birthdayLine->setText(birthday);

  if (root->friendsManager->server->serverType == LJ_TYPE) {
    trustLabel->setText(tr("Friend"));
    trustedLabel->setText(tr("Friend of"));
    watchLabel->hide();
    watchedLabel->hide();
    watchToggle->hide();
    watchedToggle->hide();
  }
  else {
    trustLabel->setText(tr("Give access"));
    trustedLabel->setText(tr("Gives you access"));
    watchLabel->show();
    watchedLabel->show();
    watchToggle->show();
    watchedToggle->show();
  }

  show();
}

void EditUser::update()
{
  bool trust_ = trustToggle->isChecked();
  bool watch_ = watchToggle->isChecked();
  QString foreground_ = foregroundLine->text().simplified();
  QString background_ = backgroundLine->text().simplified();

  if (trust != trust_ || watch != watch_ || 
      foreground != foreground_ || background != background_) {

    if (invalidColor(foreground_) || invalidColor(background_)) return;
    root->friendsManager->updateUser(username, trust_, watch_, foreground_, background_);
  }
  close();
}

void EditUser::selectColor(QLineEdit *edit)
{
  QColor color = QColorDialog::getColor(QColor(edit->text().simplified()), this);

  int r, g, b;
  color.getRgb(&r, &g, &b);
  QChar fill('0');
  QString result = QString("#%1%2%3").arg(r, 2, 16, fill).arg(g, 2, 16, fill).arg(b, 2, 16, fill);
  edit->setText(result);
}

void EditUser::selectForeground()
{
  selectColor(foregroundLine);
}

void EditUser::selectBackground()
{
  selectColor(backgroundLine);
}

bool EditUser::invalidColor(QString color)
{
  QRegExp rx("#(\\d|[A-Fa-f]){6}$");

  if (color == "") return false;

  if (!color.contains(rx)) {
    QMessageBox msgBox;
    QString message = QString(tr("Invalid color %1")).arg(color);
    msgBox.setText(message);
    msgBox.setIcon(QMessageBox::Warning);
    msgBox.exec();
    return true;
  }
  return false;
}
